<template>
  <a-spin :spinning="confirmLoading">
    <div v-if="model.id">
      <a-tabs defaultActiveKey="1">
        <a-tab-pane tab="1、基本信息" key="1"></a-tab-pane>
      </a-tabs>
    </div>
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="商机编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['num']" placeholder="系统自动生成" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                placeholder="请点击选择客户"
                v-decorator="['shortName', validatorRules.shortName]"
                :trigger-change="true"
                org-fields="id,short_name"
                dest-fields="customerId,shortName"
                code="bus_customer_select"
                @callback="popupCallback"
              />
              <a-input v-decorator="['customerId']" v-show="false"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商机名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入商机名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="销售经理" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false" v-decorator="['saleLeader', validatorRules.saleLeader]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="售前经理" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false" v-decorator="['preSaleLeader', validatorRules.saleLeader]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="产品类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['type', validatorRules.type]"
                :trigger-change="true"
                dictCode="icm_bus_business_opportunity_type"
                placeholder="请选择产品类型"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商机阶段" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                v-decorator="['stage', validatorRules.stage]"
                :trigger-change="true"
                placeholder="请选择商机阶段"
                dictCode="hf_bus_opportunity_stage,label,id"
                :disabled='!model.isEdit'
              />
            </a-form-item>
          </a-col>

          <!--          <a-col :span='12'>-->
          <!--            <a-form-item label='成单可能性' :labelCol='labelCol' :wrapperCol='wrapperCol'>-->
          <!--              <j-dict-select-tag type='list' v-decorator="['signPossibility',validatorRules.signPossibility]" :trigger-change='true'-->
          <!--                                 dictCode='icm_bus_business_opportunity_sign_possibility' placeholder='请选择成单可能性' />-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-item label="预计签单时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择预计签单时间"
                v-decorator="['expectSignTime', validatorRules.expectSignTime]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商机预算" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['budget', validatorRules.budget]"
                placeholder="请输入商机预算"
                style="width: 100%"
                type="number"
              >
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="预计毛利" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['grossProfit', validatorRules.grossProfit]"
                placeholder="请输入预计毛利"
                style="width: 100%"
                type="number"
              >
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商机来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['source', validatorRules.source]"
                :trigger-change="true"
                dictCode="icm_bus_business_opportunity_source"
                placeholder="请选择商机来源"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item v-if="model.id" label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                v-decorator="['createTime', validatorRules.startTime] " disabled
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item v-if="model.id" label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                v-decorator="['updateTime', validatorRules.startTime]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="竞争对手及产品" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea
                v-decorator="['opponentProduct', validatorRules.opponentProduct]"
                rows="3"
                placeholder="请输入竞争对手及产品"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="主要联系人" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea
                v-decorator="['mainContacts', validatorRules.mainContacts]"
                rows="3"
                placeholder="请输入主要联系人"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['remarks']" rows="3" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" type="primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <bus-business-opportunity-change-list
      :index="2"
      v-if="model.isEdit"
      :mainId="model.id"
      :dataType="'BusBusinessOpportunity'"
      ref="reGetData"
    />
    <BusBusinessSaleActivityDetailList
      :mainId="model.id"
      v-if="formDisabled"
      ref="reGetData"
    ></BusBusinessSaleActivityDetailList>
    <BusBusinessSalebeforeActivityDetailList
      :mainId="model.id"
      v-if="formDisabled"
      ref="reGetData"
    ></BusBusinessSalebeforeActivityDetailList>
    <BusBusinessDocumentDetailList
      :mainId="model.id"
      v-if="formDisabled"
      ref="reGetData"
    ></BusBusinessDocumentDetailList>
    <BusBusinessReimbursementDetailList
      :mainId="model.id"
      v-if="formDisabled"
      ref="reGetData"
    ></BusBusinessReimbursementDetailList>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import BusBusinessOpportunityChangeList from '@views/hifar/business/BusBusinessOpportunityChangeList'
import BusBusinessSaleActivityDetailList from '@views/hifar/business/BusBusinessSaleActivityDetailList'
import BusBusinessSalebeforeActivityDetailList from '@views/hifar/business/BusBusinessSalebeforeActivityDetailList'
import BusBusinessDocumentDetailList from '@views/hifar/business/BusBusinessDocumentDetailList'
import BusBusinessReimbursementDetailList from '@views/hifar/business/BusBusinessReimbursementDetailList'

export default {
  name: 'BusBusinessOpportunityForm',
  components: {
    BusBusinessOpportunityChangeList,
    BusBusinessSaleActivityDetailList,
    BusBusinessSalebeforeActivityDetailList,
    BusBusinessDocumentDetailList,
    BusBusinessReimbursementDetailList,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        saleLeader: {
          rules: [{ required: true, message: '请选择销售经理!' }],
          initialValue: '',
        },
        mainContacts: {
          rules: [{ required: true, message: '请输入主要联系人!' }],
        },
        opponentProduct: {
          rules: [{ required: true, message: '请输入竞争对手及产品!' }],
        },
        source: {
          rules: [{ required: true, message: '请选择商机来源!' }],
        },
        type: {
          rules: [{ required: true, message: '请选择产品类型!' }],
        },
        grossProfit: {
          rules: [{ required: true, message: '请输入金额!' }],
        },
        budget: {
          rules: [{ required: true, message: '请输入金额!' }],
        },
        expectSignTime: {
          rules: [{ required: true, message: '请选择预计签单时间!' }],
        },
        shortName: {
          rules: [{ required: true, message: '请点击选择客户!' }],
        },
        name: {
          rules: [{ required: true, message: '请输入商机名称!' }],
        },
        productName: {
          rules: [{ required: true, message: '请输入!' }],
        },
        stage: {
          rules: [{ required: true, message: '请选择商机阶段!' }],
        },
        signPossibility: {
          rules: [{ required: true, message: '请输入!' }],
        },
      },
      url: {
        add: '/bussiness/busBusinessOpportunity/add',
        edit: '/bussiness/busBusinessOpportunity/edit',
        queryById: '/bussiness/busBusinessOpportunity/queryById',
      },
      fields: [
        'num',
        'name',
        'customerId',
        'shortName',
        'productName',
        'mainContacts',
        'stage',
        'signPossibility',
        'expectSignTime',
        'budget',
        'grossProfit',
        'opponentProduct',
        'type',
        'source',
        'remarks',
        'saleLeader',
        'preSaleLeader',
        'createTime',
        'updateTime',
      ],
      isAdd :true
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    // inputChange(e, data) {
    //   let res = e.target.value.split(',').join('').replace(/\B(?=(\d{3})+(?!\d))/g, ',')
    //   this.$nextTick(() => {
    //     this.form.setFieldsValue({ [data]: res })
    //   })
    // },
    add() {
      this.edit({})
    },
    addRecord(record) {
      // this.$refs.reGetData.showChangeTable = false
      this.edit(record)
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
      // this.$refs.reGetData.loadData()
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
  },
}
</script>
